let fs = require("fs");
let path = require('path');

let arguments = process.argv.splice(2);
let packageTest = false;
if (arguments && arguments[0] === 'test') {
    packageTest = true;
}

//删除../webapp/react文件夹
rmdir('../webapp/react', function () {
    //复制 ./build/ 到 ../webapp/react文件夹下
    if (packageTest) {
        copyFolder('./build', '../webapp/react');
    } else {
        copyFolder('./build', '../webapp/react', false, function (file) {
            if (file.isFile()) {
                let name = file.name;
                if (name && name.endsWith('.map')) {
                    return true;
                }
            }
            return false;
        });
    }
});

function rmdir(dir, cb) {
    if (!fs.existsSync(dir)) {
        cb();
        return;
    }
    fs.readdir(dir, function (err, files) {
        next(0);

        function next(index) {
            if (index === files.length) {
                return fs.rmdir(dir, cb);
            }

            let newPath = path.join(dir, files[index]);
            fs.stat(newPath, function (err, stat) {
                if (err) {
                    console.error(err);
                }
                if (stat.isDirectory()) {
                    // 在这里完成深度优先，一层层往下找，直到找到非空文件夹
                    rmdir(newPath, () => next(index + 1));
                } else {
                    // 删除文件
                    fs.unlink(newPath, function (err) {
                        if (err) {
                            console.error(err);
                        }
                        next(index + 1);
                    });
                }
            })
        }
    });
}

function copyFolder(copiedPath, resultPath, direct, filter) {
    if (!direct) {
        copiedPath = path.join(__dirname, copiedPath);
        resultPath = path.join(__dirname, resultPath);
    }

    function createDir(dirPath) {
        fs.mkdirSync(dirPath)
    }

    if (fs.existsSync(copiedPath)) {
        createDir(resultPath);
        const files = fs.readdirSync(copiedPath, {withFileTypes: true});
        for (let i = 0; i < files.length; i++) {
            const cf = files[i];
            if (filter) {
                if (filter(cf)) {
                    continue;
                }
            }
            const ccp = path.join(copiedPath, cf.name);
            const crp = path.join(resultPath, cf.name);
            if (cf.isFile()) {
                const readStream = fs.createReadStream(ccp)
                const writeStream = fs.createWriteStream(crp)
                readStream.pipe(writeStream)
            } else {
                try {
                    /**
                     * @des 判断读(R_OK | W_OK)写权限
                     */
                    fs.accessSync(path.join(crp, '..'), fs.constants.W_OK)
                    copyFolder(ccp, crp, true, filter);
                } catch (error) {
                    console.log('folder write error:', error);
                }
            }
        }
    } else {
        console.log('do not exist path: ', copiedPath);
    }
}